import pygame

from scene.task.stage_two.task3_save_army import SaveArmy
from actor.shoot_bullet import Bullet
from scene.task.stage_one import TaskStatus
from dialog.final_dialog import FinalTask4StartDialog, FinalTaskFailDialog
from actor.sword_boom import Sword

"""
最终任务类
"""


class FinalTask(SaveArmy):

    def __init__(self, xiao_tie, japanese, mines_group, prizes_group, sword_image_group, boss_group, font,
                 obstacle_group):
        """
        初始化
        :param xiao_tie:
        :param japanese:
        :param mines_group:
        :param prizes_group:
        :param sword_group: 刀精灵组
        :param boss_group: boos精灵组
        :param font:
        :param obstacle_group:
        """
        super().__init__(xiao_tie, japanese, mines_group, prizes_group, None, None, font)
        self.sword_image_group = sword_image_group
        self.difinite_time = 2
        self.sword_group = pygame.sprite.Group()
        self.boss_group = boss_group
        self.bullet_group = pygame.sprite.Group()
        self.obstacle_group = obstacle_group
        self.sound2 = pygame.mixer.Sound("resources/sound/攻击音效.mp3")
        self.sound3 = pygame.mixer.Sound("resources/sound/射击音效.mp3")

    def draw_task(self, current_surface, x, y):
        """
        任务角色道具绘制
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        for mine in self.mines:
            mine.draw(current_surface, x, y)
        for boom in self.booms:
            boom.draw(current_surface, x, y)
        for bullet in self.bullet_group:
            bullet.draw(current_surface, x, y)
            bullet.run()
        for boss in self.boss_group:
            boss.run(None, None, self.obstacle_group)
            boss.draw(current_surface, x, y)
        for sword in self.sword_group:
            sword.draw(current_surface, x, y)
            for japan in self.japanese:
                if pygame.sprite.spritecollide(japan, self.sword_group, True):
                    japan.hp -= 75
                if japan.hp <= 0:
                    self.japanese.remove(japan)
            for boss in self.boss_group:
                if pygame.sprite.spritecollide(boss, self.sword_group, True):
                    boss.hp -= 50
            if len(self.sword_group) == 1:
                    self.sword_group.remove(sword)

    def init_dialog(self):
        """
        对话初始化
        :return:
        """
        self.task_dialog = FinalTask4StartDialog(self.font)
        self.win_dialog = None
        self.fail_dialog = FinalTaskFailDialog(self.font)

    def do_self_task(self, key_down, key_list):
        """
        任务具体实现
        :param key_down:
        :param key_list:
        :return:
        """
        for boss in self.boss_group:
            if boss.hp == 0:
                self.task_status = TaskStatus.win_dialog
        self.do_collide_group()
        if key_down and key_list[pygame.K_SPACE]:
            if self.xiao_tie.boom_count > 0:
                self.create_mine(self.xiao_tie.pos_x, self.xiao_tie.pos_y)
                self.xiao_tie.boom_count -= 1
        elif key_down and key_list[pygame.K_j] and self.xiao_tie.weapon is not None:
            if self.xiao_tie.shoot_count > 0:
                self.sound3.play()
                self.xiao_tie.shoot_count -= 1
                self.__create_bullet(self.xiao_tie.pos_x, self.xiao_tie.pos_y + 25)
        elif key_list and key_list[pygame.K_i]:
            if self.xiao_tie.weapon == '刀':
                self.sound2.play()
                self.__create_sword(self.xiao_tie.pos_x + 20, self.xiao_tie.pos_y + 25)


    def __create_bullet(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """
        bullet = Bullet(x, y, pygame.image.load("./resources/images/shoot/子弹.png"))
        bullet.set_dir(self.xiao_tie.dir)
        self.bullet_group.add(bullet)

    def __create_sword(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """

        sword_image = pygame.transform.scale(pygame.image.load("./resources/images/sword/sword.png"), (50, 40))
        sword = Sword(x, y, sword_image)
        self.sword_group.add(sword)

    def do_collide_group(self):
        """
        碰撞检测方法
        :return:
        """
        if self.xiao_tie.hp <= 0:
            self.task_status = TaskStatus.fail_dialog
        # 日军与小铁检测
        is_collide1 = pygame.sprite.spritecollide(self.xiao_tie, self.japanese, False)
        # 地雷与小铁检测
        is_collide2 = pygame.sprite.spritecollide(self.xiao_tie, self.mines_group, True)
        # 小铁与地雷检测
        is_collide3 = pygame.sprite.spritecollide(self.xiao_tie, self.mines, True)
        # 小铁和奖品类检测
        is_collide4 = pygame.sprite.spritecollide(self.xiao_tie, self.prizes_group, True)
        # 子弹和障碍物检测
        pygame.sprite.groupcollide(self.bullet_group, self.obstacle_group, True, False)
        # 小刀道具类与小铁检测
        is_collide_sword = pygame.sprite.spritecollide(self.xiao_tie, self.sword_image_group, True)
        # 地雷和日军检测
        collide_japan = pygame.sprite.groupcollide(self.mines, self.japanese, True, True)
        # 地雷与boss检测
        collide_boss = pygame.sprite.groupcollide(self.mines, self.boss_group, True, False)
        print(collide_boss)
        # 检测子弹和boss
        for boss in self.boss_group:
            if self.difinite_time == 0:
                if pygame.sprite.spritecollide(boss, self.bullet_group, True):
                    boss.hp -= 40
                self.difinite_time = 2
            else:
                self.difinite_time -= 1
        # 检测子弹和日军
        for japan in self.japanese:
            if pygame.sprite.spritecollide(japan, self.bullet_group, True):
                japan.hp -= 35
            if japan.hp <= 0:
                self.japanese.remove(japan)
        # 检测boss地雷爆炸
        for wall in collide_boss:
            self.sound1.play()
            self.create_boom(wall.rect.x - wall.rect.width,
                             wall.rect.y - wall.rect.height)
            self.xiao_tie.score += 5
            self.count += 1
            for boss in self.boss_group:
                boss.hp -= 100
        # 检测日军地雷爆炸
        for wall in collide_japan:
            self.sound1.play()
            self.create_boom(wall.rect.x - wall.rect.width,
                             wall.rect.y - wall.rect.height)
            self.xiao_tie.score += 5
            self.count += 1

        # 道具类碰撞检测
        if is_collide2 or is_collide3 or is_collide4 or is_collide_sword :
            if is_collide4:
                self.sound.play()
                self.xiao_tie.hp += 40
                self.xiao_tie.score += 1
                self.xiao_tie.shoot_count += 10
            elif is_collide_sword:
                self.xiao_tie.weapon = "刀"
            else:
                self.sound.play()
                self.xiao_tie.boom_count += 1
        # 日军碰撞检测
        if is_collide1:
            if self.xiao_tie.time_stamp == 0:
                self.xiao_tie.hp -= 25
                self.xiao_tie.time_stamp = 10
            else:
                self.xiao_tie.time_stamp -= 1
